﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace WTSQCB
{
    public partial class HeDaoTao : System.Web.UI.Page
    {
        private string chuoiketnoi = ConfigurationManager.ConnectionStrings["WebDiemTSQCBConnectionString"].ConnectionString;
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void ThemButton1_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "HeDaoTaoThem";
                cmd.Parameters.AddWithValue("@mahedaotao", MaHeDaoTaoTextBox1.Text);
                cmd.Parameters.AddWithValue("@tenhedaotao", TenHeDaoTaoTextBox2.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                HeDaoTaoGridView1.DataBind();
                Response.Write("Thêm thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void SuaButton2_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "HeDaoTaoSua";
                cmd.Parameters.AddWithValue("@mahedaotao", MaHeDaoTaoTextBox1.Text);
                cmd.Parameters.AddWithValue("@tenhedaotao", TenHeDaoTaoTextBox2.Text);
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                HeDaoTaoGridView1.DataBind();
                Response.Write("Sửa thành công");
            }
            catch
            {
                Response.Write("không thực hiện được");
            }
        }

        protected void XoaButton3_Click(object sender, EventArgs e)
        {
            try
            {
                SqlConnection con = new SqlConnection(chuoiketnoi);
                SqlCommand cmd = new SqlCommand();
                cmd.Connection = con;
                cmd.CommandType = CommandType.StoredProcedure;
                cmd.CommandText = "HeDaoTaoXoa";
                cmd.Parameters.AddWithValue("@mahedaotao", MaHeDaoTaoTextBox1.Text);                
                con.Open();
                cmd.ExecuteNonQuery();
                con.Close();
                HeDaoTaoGridView1.DataBind();
                Response.Write("Xóa thành công");
            }
            catch (Exception ex)
            {
                Response.Write("không thực hiện được");
            }
        }
        KetNoiDuLieu kn = new KetNoiDuLieu();
        protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
        {
            GridViewRow dong = HeDaoTaoGridView1.SelectedRow;
            //MaHeDaoTaoTextBox1.Text = Convert.ToString(kn.LoadData("HeDaoTaoLoad").Rows[HeDaoTaoGridView1.SelectedIndex].ItemArray[0]);
            //TenHeDaoTaoTextBox2.Text = Convert.ToString(kn.LoadData("HeDaoTaoLoad").Rows[HeDaoTaoGridView1.SelectedIndex].ItemArray[1]);
            MaHeDaoTaoTextBox1.Text = HttpUtility.HtmlDecode(dong.Cells[1].Text);
            TenHeDaoTaoTextBox2.Text = HttpUtility.HtmlDecode(dong.Cells[2].Text);
        }
    }
}